package com.zhong.gang.pda.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.zhong.gang.applib.R;
import com.zhong.gang.applib.ui.chese.MaterialTypeBean;
import com.zhong.gang.applib.utils.NumberUtils;
import com.zhong.gang.pda.ui.action.HuoBean;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


public class ShouFaView extends LinearLayout implements ViewChangeListener,View.OnClickListener, ShouFaItemView.ShouFaListener {
    private boolean isShow;
    private TextView textView;
    private boolean status;
    private ShouFaListener listener;

    public void setListener(ShouFaListener listener) {
        this.listener = listener;
    }

    public void setText(TextView text){
        if (text == null)
            return;
        textView = text;
        text.setOnClickListener(this);
    }

    public ShouFaView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.setOrientation(VERTICAL);
        TypedArray array = getContext().obtainStyledAttributes(attrs, R.styleable.ShouFaView);
        isShow = array.getBoolean(R.styleable.ShouFaView_show,true);
    }

    /**
     * 没有数据的时候添加这个时候需要用户去手动添加
     */
    public void addView(){
        ShouFaItemView faHuoItemView = new ShouFaItemView(getContext(),isShow,getChildCount(),this);
        faHuoItemView.setListener(this);
        this.addView(faHuoItemView);
        showView();
        faHuoItemView.getBinding().moneny.addTextChangedListener(faHuoItemView);
    }

    /**
     * 这个时候是详情
     */
    public void addView(HuoBean.OrderMaterialTypeDetailBean bean){
        ShouFaItemView faHuoItemView = new ShouFaItemView(getContext(),isShow,bean,getChildCount());
        faHuoItemView.setListener(this);
        this.addView(faHuoItemView);
        if (textView == null)return;
        textView.setVisibility(GONE);
        faHuoItemView.getBinding().moneny.addTextChangedListener(faHuoItemView);
        showView();
    }


    private void showView(){
        if (textView == null)return;
        if (getChildCount() >= 2 )
            textView.setVisibility(VISIBLE);
        else textView.setVisibility(GONE);
        for (int n = 0 ; n < getChildCount();n++){
            ShouFaItemView view = (ShouFaItemView) getChildAt(n);
            view.getBinding().weight.setEnabled(n<getChildCount() - 1);
            view.getBinding().weight.setHint((n<getChildCount() - 1)?"请输入重量":"");
            view.getSelect().setImageResource(n>=getChildCount() - 1? R.mipmap.icon_gou:R.drawable.bg_transparent);
            view.getSelectLayout().setEnabled(n>=getChildCount() - 1);
        }
        textView.setVisibility((status && getChildCount() > 1)?VISIBLE:GONE);
    }


    public void setEnabled(boolean status){
        this.status = status;
        for (int n = 0 ;n <getChildCount() ;n++){
            ShouFaItemView view = (ShouFaItemView) getChildAt(n);
            view.getBinding().moneny.setEnabled(status);
            view.getBinding().weight.setEnabled(n < getChildCount() -1 && status);
            view.getBinding().selectLayout.setEnabled(status);
            view.getBinding().selectLayout.setVisibility(status?VISIBLE:GONE);
            view.getBinding().type.setEnabled(status);
        }
        textView.setVisibility((status && getChildCount() > 1)?VISIBLE:GONE);
    }


    @Override
    public void onAddOrRemove(boolean isAdd) {
        if (isAdd) {
            ShouFaItemView view = (ShouFaItemView) getChildAt(getChildCount()-1);
            view.getBinding().weight.setText("");
            addView();
        }
        else onClick(null);
    }

    @Override
    public void onClick(View view) {
        if (getChildCount() > 1 ){
            this.removeViewAt(getChildCount() -1);
        }
        showView();
    }

    public String getJsonArray(){
        JSONArray array = new JSONArray();
        for (int n = 0 ; n < getChildCount();n++){
            View view = getChildAt(n);
            if (view instanceof ShouFaItemView) {
                try {
                    ShouFaItemView faItemView = (ShouFaItemView) view;

                    String weightStr = faItemView.getBinding().weight.getText().toString();
                    JSONObject object = new JSONObject();
                    object.put("materialTypeId",faItemView.getItemId());

                    HuoBean.OrderMaterialTypeDetailBean.MaterialTypeBean materialType = new HuoBean.OrderMaterialTypeDetailBean.MaterialTypeBean();
                    HuoBean.OrderMaterialTypeDetailBean.MaterialTypeBean.MaterialTypePriceBean materialTypePrice
                            = new HuoBean.OrderMaterialTypeDetailBean.MaterialTypeBean.MaterialTypePriceBean();
                    materialTypePrice.setMaterialYardPrice(faItemView.getPrice());
                    materialType.setMaterialTypePrice(materialTypePrice);
                    object.put("materialType",new JSONObject(new Gson().toJson(materialType)));

                    object.put("materialTypeName",faItemView.getBinding().typeName.getText().toString());
                    double price = 0;
                    try {
                        price = NumberUtils.getNumber(faItemView.getBinding().moneny.getText().toString());
                    }catch (Exception e){}
                    object.put("price",price+"");
                    object.put("weight", TextUtils.isEmpty(weightStr)?0:weightStr);
                    array.put(object);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }
        return array.toString();
    }

    @Override
    public void onShowFa() {
        if (listener != null) {
            double money = 0.0;
            for (int n = 0 ; n < getChildCount();n++){
                ShouFaItemView view = (ShouFaItemView) getChildAt(n);
                if (view instanceof ShouFaItemView) {
                    double price = 0;
                    double weight = 0;
                    try {
                        price = Double.parseDouble(view.getBinding().moneny.getText().toString());
                        weight = Double.parseDouble(view.getBinding().weight.getText().toString());
                        money =money+ price*weight;
                    }catch (Exception e){}
                }
            }
            listener.onShowFa(NumberUtils.getNumber(money));
        }
    }



    public interface ShouFaListener{
        void onShowFa(double moneny);
    }
}
